package com.gmrz.webauthn.protocol.v1.processor;

import com.gmrz.uaf.common.IErrorCode;


public enum WebAuthnErrorCode
        implements IErrorCode {
    UNKNOWN(-1, "Unknown error code", 1500),
    STATUS_INVALID_WEBAUTHN_REQUEST(1, "Invalid Web Authentication Request.", 1498),
    STATUS_INVALID_WEBAUTHN_RESPONSE(2, "Invalid Web Authentication Response.", 1498),

    STATUS_CLIENTDATA_DESERIALIZATION_FAILED(3, "ClientData is not UTF-8 encoded", 1498),
    STATUS_CLIENTDATA_HASHING_FAILED(4, "Failed to compute the clientDataHash.", 1498),
    STATUS_CLIENTDATA_VALIDATION_FAILED(5, "Failed to validate the ClientData.", 1498),
    STATUS_CLIENTDATA_HASH_VALIDATION_FAILED(6, "Failed to validate the clientDataHash.", 1498),
    STATUS_CHALLENGE_VALIDATION_FAILED(7, "Failed to validate challenge.", 1498),
    STATUS_FACETID_VALIDATION_FAILED(8, "Failed to validate facetID.", 1498),
    STATUS_CHANNELID_VALIDATION_FAILED(9, "Failed to validate ChannelID.", 1290),
    STATUS_CLIENTDATA_TYPE_VALIDATION_FAILED(10, "Failed to validate type", 1498),

    STATUS_PROTOCOL_VERSION_NOT_SUPPORTED(11, "Protocol version is not supported.", 1497),

    STATUS_SERVERDATA_VALIDATION_FAILED(12, "Failed to validate the ServerData.", 1498),

    STATUS_PARSING_TPM_ATTESTATION_FAILED(13, "Failed to parse TPM attestation", 1498),
    STATUS_TPM_ATTESTATION_VALIDATION_FAILED(14, "Failed to validate TPM attestation", 1498),
    STATUS_TMP_VERSION_NOT_SUPPORTED(15, "TPM version is not supported", 1498),

    STATUS_INSUFFICIENT_CREDENTIAL_ID_LENGTH(16, "The credential id length is too short", 1498),
    STATUS_INVALID_CREDENTIAL_TYPE(17, "The credential type is not supported", 1498),

    STATUS_INVALID_PUBLIC_KEY(18, "The public key is invalid", 1498),

    STATUS_ATTESTATION_TYPE_NOT_SUPPORTED(19, "Attestation type not supported.", 1498),
    ERROR_ATTESTATION_MISSING(20, "Attestation is missing.", 1496),
    ERROR_ATTESTATION_UNKNOWN_FORMAT(21, "Failed to recognize the attestation format.", 1496),
    ERROR_ATTESTATION_MALFORMED_STATEMENT(22, "Failed to validate the attestation statement.", 1496),
    ERROR_ATTESTATION_CERT_VALIDATION_FAILED(23, "Failed to validate the attestation certificate.", 1496),
    ERROR_ATTESTATION_SIGNATURE_VALIDATION_FAILED(24, "Failed to validate the attestation signature.", 1496),
    ERROR_TRUST_ANCHOR_NOT_FOUND(25, "Failed to locate the trust anchor in authenticator metadata.", 1496),

    STATUS_REGDATA_VALIDATION_FAILED(26, "Failed to validate registration data.", 1492),
    STATUS_SIGDATA_VALIDATION_FAILED(27, "Failed to validate signature data.", 1492),
    STATUS_INVALID_AUTHENTICATOR_DATA(28, "Failed to parse registration data.", 1498),
    STATUS_INVALID_SIGNATURE_DATA(29, "Failed to parse signature data.", 1498),
    STATUS_POLICY_NOT_FOUND(30, "Requested policy is not available on server.", 1492),
    STATUS_POLICY_INVALID(31, "Failed to verify the server policy.", 1492),
    STATUS_POLICY_NO_MATCHING_AUTHENTICATORS(32, "Defined policy has no matching authenticators.", 1492),
    STATUS_TLSDATA_VALIDATION_FAILED(33, "Failed to validate ChannelBinding data.", 1290),

    STATUS_POLICY_VALIDATION_FAILED(34, "Failed to validate authenticator assertion against the server policy.", 1492),
    ERROR_POLICY_VERIFICATION_FAILED(35, "Policy Verification Failed.", 1492),

    STATUS_APPID_VALIDATION_FAILED(36, "Failed to validate AppID.", 1498),

    ERROR_AUTHENTICATION_MODE_INVALID(37, "Failed to validate authentication mode.", 1492),
    ERROR_METADATA_NOT_FOUND(38, "Failed to locate authenticator metadata.", 1480),
    ERROR_INVALID_REG_ASSERTION(39, "Failed to parse registration assertion TLV.", 1498),
    ERROR_INVALID_AUTH_ASSERTION(40, "Failed to parse authentication assertion TLV.", 1498),
    ERROR_KEY_FORMAT_NOT_FOUND(41, "Failed to locate the key format in authenticator metadata.", 1494),
    ERROR_AUTHENTICATOR_NOT_FOUND(42, "Failed to locate a registered authenticator.", 1481),
    ERROR_ALGORITHM_NOT_FOUND(43, "Failed to locate authentication algorithm in authenticator metadata.", 1495),
    ERROR_INVALID_SIGN_COUNTER(44, "Invalid value of sign counter.", 1492),
    STATUS_USER_NOT_FOUND(45, "Failed to locate registered user.", 1481),
    STATUS_AUTHENTICATOR_STATE_INVALID(46, "Authenticator state is not valid.", 1493),

    ERROR_SIGNATURE_VALIDATION_FAILED(47, "Failed to validate the signature.", 1496),

    ERROR_DATABASE_WRITE_ERROR(48, "Failed to write to database.", 1500),
    ERROR_DATABASE_READ_ERROR(49, "Failed to read from database.", 1500),
    ERROR_DATABASE_CONNECTION_ERROR(50, "Failed to connect to database.", 1500),

    STATUS_USERNAME_INVALID(51, "Invalid userName", 1401),

    INVALID_METADATA_VERSION(52, "Invalid metadata version.", 1492),

    STATUS_CHALLENGE_EXPIRED(53, "Failed to validate the ServerData.", 1498),
    STATUS_REPLAY_ATTACK(54, "Possible Replay Attack", 1498),
    ERROR_AUTHENTICATOR_VERSION_INVALID(55, "Failed to validate authenticator version", 1492),
    ERROR_MAXIMUM_AUTHENTICATORS_LIMIT_REACHED(56, "Maximum limit for number of authenticators for this user reached.", 1498),
    ERROR_MAXIMUM_AUTHENTICATORS_LIMIT_EXCEEDED(57, "Registered authenticators count for this user is already exceeding the allowed authenticators limit.", 1498),
    ERROR_MAXIMUM_AUTHENTICATORS_LIMIT_WILL_EXCEED(58, "Maximum limit for number of authenticators for this user will exceed on completing registration.", 1498),

    ERROR_CONFIG_PROPERTY_VALUE_INVALID(59, "Config property value is invalid or missing", 1500),

    ERROR_CBOR_DECODING_FAILED(60, "Failed to decode the CBOR", 1498),
    ERROR_CBOR_ENCODING_FAILED(61, "Failed to encode the CBOR", 1498),
    STATUS_INVALID_ATTESTATION_DATA(62, "Failed to get AttestationData bytes", 1498),
    ERROR_WEBAUTHN_REQUEST_GENERATION_FAILED(63, "Web Authen Request generation failed", 1498),
    STATUS_OPTIONAL_POLICY_CHECK_FAILED(64, "Optional policy check failed.", 1298),
    STATUS_INVALID_EXTENSION(65, "Failed to get extension bytes", 1498),
    ERROR_AUTHENTICATOR_REVOKED(66, "Authenticator is revoked", 1493),

    ERROR_AUTHENTICATOR_SPEC_INVALID(67, "Unsupported value in spec", 1500),

    STATUS_EXISTING_CREDENTIAL_ID(68, "Credential id already present.", 1498),
    STATUS_RPID_VALIDATION_FAILED(69, "Failed to validate rpID hash in AuthenticatorData.", 1498),
    STATUS_UV_VALIDATION_FAILED(70, "Failed to validate uv in AuthenticatorData.", 3001);

    private int code;
    private int statusCode;
    private String message;

    private WebAuthnErrorCode(int code, String message, int statusCode) {
        this.code = code;
        this.message = message;
        this.statusCode = statusCode;
    }

    public int getCode() {
        return this.code;
    }

    public int getStatusCode() {
        return this.statusCode;
    }

    public String getMessage() {
        return this.message;
    }

    public String toString() {
        return "ErrorCode=[" + this.code + "], Message=[" + this.message + "]";
    }

    public static WebAuthnErrorCode forErrorCode(int errorCode) {
        for (WebAuthnErrorCode u2fEc : values()) {
            if (u2fEc.code == errorCode) {
                return u2fEc;
            }
        }
        return UNKNOWN;
    }
}
